package com.bl.weilan.service.biz;

import com.bl.weilan.common.ApiResponseCode;
import com.bl.weilan.common.CommonServiceException;
import com.bl.weilan.service.vo.ListVo;
import org.springframework.data.domain.Page;

import java.util.List;

/**
 * Created by liangweikang on 2017/12/10.
 */
public abstract class BaseService {
    public <T> ListVo handleListWithPageable(Page<T> listWithPageable) {
        ListVo<T> listVo = new ListVo<T>();
        listVo.setTotal(listWithPageable.getTotalElements());
        listVo.setList(listWithPageable.getContent());
        return listVo;
    }

    public <T> ListVo handleList(List<T> list) {
        ListVo<T> listVo = new ListVo<T>();
        listVo.setTotal((long) list.size());
        listVo.setList(list);
        return listVo;
    }

    protected void throwCommonServiceException(ApiResponseCode apiResponseCode) {
        throw new CommonServiceException(apiResponseCode.getCode(), apiResponseCode.getMsg());
    }
}
